package com.cskaoyan.service.promotion;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.promotion.coupon.*;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.util.PageUtil;
import com.cskaoyan.util.UUIDUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 优惠卷管理
 * @Author: WD created on 2021-09-16 17:01
 * @Version:
 */

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public ShowCouponListVO getCouponList(BaseParam param, String name, Short type, Short status) {
        PageUtil.start(param);
        CouponExample couponExample = new CouponExample();
        couponExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        CouponExample.Criteria criteria = couponExample.createCriteria();
        if (StringUtil.isNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        if (type != null) {
            criteria.andTypeEqualTo(type);
        }

        if (status != null) {
            criteria.andStatusEqualTo(status);
        }

        //剔除逻辑删除的
        criteria.andDeletedEqualTo(false);

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);

        ArrayList<CouponListVO> couponListVOs = new ArrayList<>();

        for (Coupon coupon : coupons) {
            //判断是否过期

            if (coupon.getStatus() == 0 && coupon.getEndTime() != null && coupon.getEndTime().getTime() <= new Date().getTime()) {
                Integer id = coupon.getId();
                //更新coupon表
                couponMapper.UpdateStatusById(id);
                //更新coupon-user表
                couponUserMapper.updateStatusByCouponId(id);
                coupon.setStatus((short) 1);
            }


            couponListVOs.add(new CouponListVO(coupon));
        }
        return new ShowCouponListVO(couponPageInfo, couponListVOs);
    }


    @Override
    public GetCouponInfoVO getCouponById(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);

        return new GetCouponInfoVO(coupon);

    }

    @Override
    public ShowCouponUserListVO getCouponUserList(BaseParam param, Integer couponId, Integer userId, Short status) {
        PageUtil.start(param);

        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        if (couponId != null) {
            criteria.andCouponIdEqualTo(couponId);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>(couponUsers);
        ArrayList<CouponUserVO> list = new ArrayList<>();

        for (CouponUser couponUser : couponUsers) {
            list.add(new CouponUserVO(couponUser));
        }

        return new ShowCouponUserListVO(couponUserPageInfo, list);

    }

    @Override
    public UpdateCouponVO updateCoupon(UpdateCouponBO couponBO) {
        Coupon coupon = new Coupon(couponBO);
        if(coupon.getType() ==2){
            Coupon coupon1 = couponMapper.selectByPrimaryKey(coupon.getId());
            if(coupon1.getCode()==null){

                //生成优惠卷代码
                coupon.setCode(UUIDUtil.getCouponUUID());
            }
        }


        //优惠卷更新后更新相应的时间
        coupon.checkUpdateDay(coupon);
        CouponUser couponUser = new CouponUser(coupon);
        couponUserMapper.updateAfterCouponUpdated(couponUser);

        couponMapper.updateByPrimaryKey(coupon);

        return new UpdateCouponVO(coupon);
    }

    @Override
    public Integer deleteCouponNotReal(DeleteCouponVO couponVO) {
        Integer id = couponVO.getId();
        //规定用户手里有未使用且未过期的优惠卷不能删除
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(id);
        criteria.andStatusEqualTo((short) 0);
        criteria.andDeletedEqualTo(false);
        long l = couponUserMapper.countByExample(couponUserExample);
        if (l > 0) {
            return -1;
        }
        CouponUser record = new CouponUser();
        record.setStatus((short) 3);
        CouponUserExample couponUserExample1 = new CouponUserExample();
        CouponUserExample.Criteria criteria1 = couponUserExample1.createCriteria();
        criteria1.andCouponIdEqualTo(id);
        criteria1.andDeletedEqualTo(false);
        couponUserMapper.updateByExampleSelective(record,couponUserExample1);
        Integer integer = couponMapper.deleteCouponNotRealById(id);
        return integer;


    }

    @Override
    public CreateCouponVO createCoupon(CreateCouponBO couponBO) {
        Coupon coupon = new Coupon(couponBO);
        if(coupon.getStatus()==2){
            coupon.setCode(UUIDUtil.getCouponUUID());
        }
        coupon.checkUpdateDay(coupon);
        int i = couponMapper.insertSelective(coupon);
        Integer id = coupon.getId();

        return new CreateCouponVO(coupon);
    }
}
